/**
 * 
 */
package neptune.web.client.view;

import java.util.ArrayList;

/**
 * This class is used to manage history tokens
 * 
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class HistoryTokenUtils {
	// La stringa usata in PREFIX_SEPARATOR e STATE_SEPARATOR viene data in
	// pasto ad un elaboratore di espressioni regolari, quindi prestare
	// attenzione a particolari sequenze di caratteri che possono essere
	// interpretate come un particolare comando.
	public static final String PREFIX_SEPARATOR = "___";
	public static final String STATE_SEPARATOR = ":::";

	private static ArrayList<String> registeredTokens = new ArrayList<String>(0);

	/**
	 * Builds an history token, given the token and its prefix
	 * 
	 * @param prefix
	 * @param token
	 * @return
	 */
	public static String buildToken(String prefix, String token) {
		if (prefix.equals(""))
			return token;

		return prefix + PREFIX_SEPARATOR + token;
	}

	/**
	 * Builds an history token, given the token and its prefix. At the end of the history token is
	 * appended the state string
	 * 
	 * @param prefix
	 * @param token
	 * @param state
	 * @return
	 */
	public static String buildToken(String prefix, String token, String state) {
		if (prefix.equals(""))
			return token + STATE_SEPARATOR + state;

		return prefix + PREFIX_SEPARATOR + token + STATE_SEPARATOR + state;
	}

	/**
	 * Adds the state string to an HistoryToken
	 * 
	 * @param token
	 * @param state
	 * @return
	 */
	public static String addStateToToken(String token, String state) {
		return token + STATE_SEPARATOR + state;
	}

	/**
	 * Registers the view name
	 * 
	 * @param token
	 * @throws DuplicatedTokenException
	 */
	public static void registerViewToken(String token)
			throws DuplicatedTokenException {
		if (!registeredTokens.contains(token))
			registeredTokens.add(token);
		else
			throw new DuplicatedTokenException("The token \"" + token
					+ "\" is already registered");
	}

	/**
	 * Unregisters the view name
	 * 
	 * @param token
	 */
	public static void unregisterViewToken(String token) {
		registeredTokens.remove(token);
	}

	/**
	 * Extract the state string from the given HistoryToken.
	 * Returns null if there isn't state string
	 * 
	 * @param tokenString
	 * @return
	 */
	public static String getStateString(String tokenString) {
		if(tokenString.split(HistoryTokenUtils.STATE_SEPARATOR,2).length < 2)
			return null;
		
		return tokenString.split(HistoryTokenUtils.STATE_SEPARATOR,2)[1];
	}

	/**
	 * Returns an array representing the views hierarchy.
	 * 
	 * @param tokenString
	 * @return
	 */
	public static String[] getTokenHierarchy(String tokenString) {
		String[] gerarchy = tokenString
				.split(HistoryTokenUtils.PREFIX_SEPARATOR);
		gerarchy[gerarchy.length - 1] = gerarchy[gerarchy.length - 1]
				.split(HistoryTokenUtils.STATE_SEPARATOR)[0];

		return gerarchy;
	}
}
